/*
 * Licensed Materials - Property of tenxcloud.com
 * (C) Copyright 2023 TenxCloud. All Rights Reserved.
 * 2023-02-09  @author xyinting
 */

package users

import (
	"errors"

	"github.com/golang/glog"
)

type UserRequest struct {
	Account         string `json:"account"`
	Password        string `json:"password"`
	ConformPassword string `json:"conformPassword"`
	Phone           string `json:"phone"`
	ReferralCode    string `json:"referralCode,omitempty"`
	VerifyCode      string `json:"verifyCode"`
}

func (ur *UserRequest) Verify() error {
	if ur.Account == "" {
		glog.Infof("account can not empty")
		return errors.New("account can not empty")
	}

	if ur.Password != ur.ConformPassword {
		glog.Infof("password can not match, please check conformPassword")
		return errors.New("password can not match, please check conformPassword")
	}

	if ur.Phone == "" {
		glog.Infof("phone can not empty")
		return errors.New("phone can not empty")
	}

	if !ur.VerifyPhoneCode() {
		glog.Infof("check phone verify code failed, phone %s, code %s", ur.Phone, ur.VerifyCode)
		return errors.New("check phone verify code failed")
	}

	return nil
}

func (ur *UserRequest) VerifyPhoneCode() bool {
	// TODO verify phone code
	//rediscli := utils.GetRedis()
	//code, err := rediscli.Get(context.Background(), utils.SMSCodeKey(ur.Phone)).Result()
	//if err != nil {
	//	glog.Errorf("get verify code failed, error %v", err)
	//	return false
	//}
	//
	//if code != ur.VerifyCode {
	//	glog.Warningf("verify code failed, get %s but %s", code, ur.VerifyCode)
	//	return false
	//}
	return true
}
